package com.project8.book;

import java.io.Serializable;
import java.util.ArrayList;

/** A Chapter is essentially a collection of Pages -- There is some redundancy with Book class */
public class Chapter implements Serializable 
{	
		private static final long serialVersionUID = 3368434766417086258L;

		public static long chapter_id = 0;
		
		long id;
		long chapterNum = 0;		//Not every chapter requires a number, default = 0
		String chapterName;
		String folderName;
		

		Page chapterStart;
		Page chapterEnd;
		
		ArrayList<Page> pagesInChapter = new ArrayList<Page>();
		ArrayList<Figure> figuresInChapter = new ArrayList<Figure>();
		
		
		
		public Chapter(String chName)
		{
			id = chapter_id++;
			
			chapterName = chName.substring(1);
			folderName = chName;
		}
		
		public Chapter(String chName, Page beginning, Page end)
		{
			id = chapter_id++;
			
			chapterName = chName;
			chapterStart = beginning;
			chapterEnd = end;
		}
		
		public Chapter(long chNum, String chName, Page beginning, Page end)
		{
			id = chapter_id++;
			
			chapterName = chName;
			chapterStart = beginning;
			chapterEnd = end;
			chapterNum = chNum;
		}
		
		public void setNum(long chNum)
		{
			chapterNum = chNum;
		}
		
		public long getId()
		{
			return id;
		}
		
		public String getName()
		{
			return chapterName;
		}
		
		public long getNum()
		{
			return chapterNum;
		}
		
		public Page getFirstPage()
		{
			return getPages().get(0);
		}
		
		public Page getlastPage()
		{
			return getPages().get(pagesInChapter.size()-1);
		}
		
		/** Add a new Page to this Chapter */
		public void addOnePage(Page newPage)
		{
			newPage.setChapterName(this.chapterName);
			newPage.setChapterNum(chapterNum);
			
			pagesInChapter.add(newPage);
						
			sortPages();
		}
		
		/** Add a collection of new Pages to this Chapter */
		public void addMultiplePages(ArrayList<Page> newPages)
		{
			for(int i = 0; i < newPages.size(); i++)
			{
				newPages.get(i).setChapterName(this.chapterName);
				newPages.get(i).setChapterNum(this.chapterNum);
				
				pagesInChapter.add((Page)newPages.get(i));
			}
			
			sortPages();
		}
		
		/** Remove a page from this Chapter -- There shouldn't ever be a need to call this method */
		public void removeOnePage(Page pageToRemove)
		{
			if(pagesInChapter.contains(pageToRemove))
			{
				int index = pagesInChapter.indexOf(pageToRemove);
				pagesInChapter.remove(index);
			}
		}
		
		/** Sorts the Pages in this Chapter so that they are always in order of increasing page number */
		private void sortPages()
		{
			for(int i = 0; i < pagesInChapter.size() - 1; i++)
				for(int j = i; j < pagesInChapter.size(); j++)
				{
					if(((Page)pagesInChapter.get(j)).getPageNum() < ((Page)pagesInChapter.get(i)).getPageNum())
					{
						Page temp = (Page)pagesInChapter.get(i);
						pagesInChapter.set(i, (Page)pagesInChapter.get(j));
						pagesInChapter.set(j, temp);
					}
				}
		}
		
		public ArrayList<Page> getPages()
		{
			return pagesInChapter;
		}
		
		public long numPages()
		{
			return pagesInChapter.size();
		}
		
		@Override
		public String toString()
		{
			return chapterName;
		}
		
		@Override
		public boolean equals(Object o)
		{
			Chapter anotherChapter = (Chapter)o;
			
			if(this.getName().equals(anotherChapter.getName()) && this.getId() == anotherChapter.getId())
				return true;
						
			return false;
		}
		
		/* getAllFigures() will return an ArrayList of the names of images which appear 
		 * on each page of the book for this specific chapter.
		 * 
		 * Author: bcollins
		 */
		public ArrayList<Figure> getAllFigures()
		{
			figuresInChapter.clear();
			int figNum = 1;
						
			for(int i = 0; i < pagesInChapter.size(); i++)
			{	
				ArrayList<String> figures = pagesInChapter.get(i).getFiguresOnPage();
				ArrayList<String> captions = pagesInChapter.get(i).getFigureCaptions();
				
				for(int j = 0; j < pagesInChapter.get(i).getFiguresOnPage().size(); j++)
				{										
					Figure oneFig = new Figure(figures.get(j), pagesInChapter.get(i), captions.get(j), figNum);
					
					figNum++;
					
					figuresInChapter.add(oneFig);
				}				
			}
			
			return figuresInChapter;
		}
		
		public String getFolderName() {
			return folderName;
		}
}
